const CompressionPlugin = require("compression-webpack-plugin");

const path = require("path");

process.env.VUE_APP_VERSION = process.env.npm_package_version;

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: "/",

  devServer: {
    proxy: {
      "/": {
        target: "http://192.168.0.105:8870",
      },
    },
    port: 8008,
  },

  configureWebpack: {
    devServer: {
      disableHostCheck: true,
    },
    devtool: "source-map",
  },

  chainWebpack(config) {
    config.module
      .rule("svg")
      .exclude.add(resolve("src/assets/svg"))
      .end();
    config.module
      .rule("icon")
      .test(/\.svg$/)
      .include.add(resolve("src/assets/svg"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();
    config.plugins.delete("prefetch");
    if (process.env.NODE_ENV === "production") {
      config.externals({
        vue: "Vue",
        axios: "axios",
        "element-ui": "ELEMENT",
        "vue-router": "VueRouter",
        vuex: "Vuex",
        wangeditor: "wangEditor",
        echarts: "echarts",
        "v-charts": "VeIndex",
      });
      const cdn = {
        css: [
          // element-ui css
          "https://unpkg.com/element-ui@2.15.6/lib/theme-chalk/index.css",
        ],
        js: [
          // vue
          "https://unpkg.com/vue@2.6.14/dist/vue.min.js",
          // vue router
          "https://unpkg.com/vue-router@3.5.1/dist/vue-router.min.js",
          // vuex
          "https://unpkg.com/vuex@3.6.2/dist/vuex.min.js",
          // element-ui js
          "https://unpkg.com/element-ui@2.15.6/lib/index.js",
          // axios
          "https://unpkg.com/axios@0.21.1/dist/axios.min.js",
          // wangEditor
          "https://unpkg.com/wangeditor@4.7.8/dist/wangEditor.min.js",
          // echarts
          "https://unpkg.com/echarts@4.8.0/dist/echarts.min.js",
          // v-charts
          "https://unpkg.com/v-charts@1.19.0/lib/index.min.js",
        ],
      };
      config.plugin("html").tap((args) => {
        args[0].cdn = cdn;
        return args;
      });
      config.plugin("CompressionPlugin").use(CompressionPlugin);
    }
  },

  productionSourceMap: true,
};
